package com.example.blog_system_ssm.controller;

import com.example.blog_system_ssm.component.AjaxResult;
import com.example.blog_system_ssm.component.ApplicationVariable;
import com.example.blog_system_ssm.component.StringTools;
import com.example.blog_system_ssm.component.UserSessionTools;
import com.example.blog_system_ssm.entity.ArticleEntity;
import com.example.blog_system_ssm.entity.UserEntity;
import com.example.blog_system_ssm.entity.vo.ArticleEntityVO;
import com.example.blog_system_ssm.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/art")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping("/add")
    public AjaxResult add(ArticleEntity articleEntity, HttpServletRequest request){
        // 1,非空校验
        if(articleEntity == null || !StringUtils.hasLength(articleEntity.getTitle())
                || !StringUtils.hasLength(articleEntity.getContent()))
            return AjaxResult.fail(-1,"参数错误");

        // 2,组装数据，得到Uid
        UserEntity userEntity = UserSessionTools.getLoginUser1(request);
        articleEntity.setUid(userEntity.getId());

        // 3,持久化，将结果返回至前端
        int result = articleService.add(articleEntity);
        return AjaxResult.success(result);
    }

    // 获取文章的详情信息，但需要进行鉴权（判断权限的归属人是不是当前登录用户）
    @RequestMapping("/getdetailbyid")
    public AjaxResult getDetailByIdAndUid(Integer id,HttpServletRequest request){
        // 1，非空校验
        if(id == null || id <= 0)
            return AjaxResult.fail(-1,"非法参数");
        UserEntity userEntity = UserSessionTools.getLoginUser1(request);
        return AjaxResult.success(articleService.getDetailByIdAndUid(id, userEntity.getId()));
    }

    // 更新文章功能
    @RequestMapping("/update")
    public AjaxResult update(ArticleEntity articleEntity,HttpServletRequest request){
        // 1，非空校验
        if(null == articleEntity || articleEntity.getId() <= 0
                || !StringUtils.hasLength(articleEntity.getTitle())
                || !StringUtils.hasLength(articleEntity.getContent()))
            return AjaxResult.fail(-1,"参数有误！");
        // 2.获取登录用户的id,articleinfo 对象中（修改是要验证权限）
        UserEntity userEntity = UserSessionTools.getLoginUser(request);
        articleEntity.setUid(userEntity.getId());
        articleEntity.setUpdatetime(LocalDateTime.now());
        // 得到受影响的行数
        int result = articleService.update(articleEntity);
        return AjaxResult.success(result);
    }

    // 得到文章详情页功能
    @RequestMapping("/getdetail")
    public AjaxResult getDetail(Integer id){
        if(id == null || id <= 0)
            return AjaxResult.fail(-1,"参数有误！");
        ArticleEntityVO articleEntityVO = articleService.getDetail(id);
        // 获取用户的文章总篇数
        int artCount = articleService.getPageCountByUser(articleEntityVO.getUid());
        articleEntityVO.setArtCount(artCount);
        if (articleEntityVO!=null && StringUtils.hasLength(articleEntityVO.getUsername())){
            return AjaxResult.success(articleEntityVO);
        }
        return AjaxResult.fail(-1,"参数有误！");
    }

    // 文章浏览量添加功能
    @RequestMapping("/addrcount")
    public AjaxResult addRCount(Integer id){
        if(id == null || id <= 0)
            return AjaxResult.fail(-1,"参数有误！");
        int result = articleService.addRCount(id);
        return AjaxResult.success(result);
    }

    // 个人博客列表页功能
    @RequestMapping("/mylist")
    public AjaxResult getMyList(HttpServletRequest request){
        UserEntity userEntity = UserSessionTools.getLoginUser1(request);
        List<ArticleEntity> list = articleService.getArtListByUid(userEntity.getId());
        // todo: 将文章正文截取成文章摘要
        for (ArticleEntity item: list) {
            String content = StringTools.subLength(item.getContent(),200);
            // todo: 将摘要里面的 markdown 关键字去除掉
            item.setContent(content);
        }
        return AjaxResult.success(list);
    }

    // 删除文章功能
    @RequestMapping("/del")
    public AjaxResult delArt(Integer id,HttpServletRequest request){
        if(id == null || id <= 0)
            return  AjaxResult.fail(-1,"参数有误！");
        UserEntity userEntity = UserSessionTools.getLoginUser1(request);
        int result = articleService.delArt(id,userEntity.getId());
        return AjaxResult.success(result);
    }

    @RequestMapping("/getlistbypage")
    public AjaxResult getArtListByPage(Integer pageSize,Integer pageIndex){
        // 默认值处理 pageSize pageIndex
        if(pageSize==null || pageSize <= 0){
            pageSize = 4;
        }
        if(pageIndex==null || pageIndex <= 0){
            pageIndex = 1;
        }
        // 分页公式
        int offset = (pageIndex-1) * pageSize;
        List<ArticleEntity> list = articleService.getArtListByPage(pageSize,offset);
        // 多线程并发循环执行文章简介功能
        list.stream().parallel().forEach(item -> {
            item.setContent(StringTools.subLength(item.getContent(),150));
        });
        return AjaxResult.success(list);
    }

    // 获取文章总篇数
    @RequestMapping("/getcount")
    public AjaxResult getPageCount(){
        int result = articleService.getPageCount();
        return AjaxResult.success(result);
    }

    // 获取用户的个人列表页
    @RequestMapping("/mylistbypage")
    public AjaxResult getMyArtListByPage(Integer pageSize,Integer pageIndex,HttpServletRequest request){
        UserEntity userEntity = UserSessionTools.getLoginUser1(request);
        // 默认值处理 pageSize pageIndex
        if(pageSize==null || pageSize <= 0){
            pageSize = 4;
        }
        if(pageIndex==null || pageIndex <= 0){
            pageIndex = 1;
        }
        // 分页公式
        int offset = (pageIndex-1) * pageSize;
        List<ArticleEntity> list = articleService.getMyArtListByPage(pageSize,offset,userEntity.getId());
        // 多线程并发循环执行文章简介功能
        list.stream().parallel().forEach(item -> {
            // todo: 将文章正文截取成文章摘要
            item.setContent(StringTools.subLength(item.getContent(),200));
            // todo: 将摘要里面的 markdown 关键字去除掉
        });
        return AjaxResult.success(list);
    }

    // 获取用户的文章总篇数
    @RequestMapping("/getcountbyuser")
    public AjaxResult getPageCountByUser(HttpServletRequest request){
        UserEntity userEntity = UserSessionTools.getLoginUser1(request);
        int result = articleService.getPageCountByUser(userEntity.getId());
        return AjaxResult.success(result);
    }
}
